package cc.javaee.bbs.controller.api;

import cc.javaee.bbs.constants.Constants;
import cc.javaee.bbs.dao.activity.BankuaiCustomerService;
import cc.javaee.bbs.model.Huifu;
import cc.javaee.bbs.model.Tiezi;
import cc.javaee.bbs.model.activity.BankuaiCustomer;
import cc.javaee.bbs.model.activity.KbrActivityCustomer;
import cc.javaee.bbs.service.HuifuService;
import cc.javaee.bbs.service.TieziService;
import cc.javaee.bbs.service.activity.ActivityService;
import cc.javaee.bbs.tool.FileUpload;
import cc.javaee.bbs.vo.ResultVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xdw
 */
@Controller
@RequestMapping("/appInteface/huifu")
public class HuifuController {

    @Autowired
    HuifuService huifuService;
    @Autowired
    ActivityService activityService;
    @Autowired
    TieziService tieziService;
    @Autowired
    BankuaiCustomerService bankuaiCustomerService;

    // http://localhost:8080/bbs//appInteface/huifu/add/{source}/{id}

    /**
     * 回复接口
     */
    @ResponseBody
    @RequestMapping("/add/{source}/{id}")
    public ResultVO add(@PathVariable("source") String source,
                        @PathVariable("id") Integer id, Huifu huifu,
                        @RequestParam("archivalNumber") String archivalNumber,
                        @RequestParam(value = "huifuid", required = false) Integer huifuid,
                        @RequestParam(value = "context", required = false) String context,
//                        @RequestParam(value = "file", required = false) MultipartFile file,
                        @RequestParam(value = "fileName", required = false) String[] fileName) {

        BankuaiCustomer customer = bankuaiCustomerService.findByArchivalNumber(archivalNumber);
        if (customer == null) {
            return ResultVO.error(-1, "未加入圈子");
        }

//        if (file != null) {
//            try {
//                String filePath = FileUpload.upload(file);
//                huifu.setImg(filePath);
//            } catch (IOException e) {
//                return ResultVO.error(-1, "文件上传失败");
//            }
//        }
        if (fileName != null) {
            if (fileName.length > 3) {
                return ResultVO.error(-1, "图片不能大于3个");
            }
            if (fileName.length > 0) {
                List<String> strings = Arrays.asList(fileName);
                String join = String.join(",", strings);
                huifu.setImg(join);
            }
        }
        huifu.setId(null);

        if (huifuid != null && huifuid != 0) {
            huifu.setHuifuid(huifuid);
        }
        if ("huifu".equals(source)) {
            huifu.setHuifuid(id);
        } else if ("circle".equals(source)) {
            huifu.setTieziid(id);
        } else {
            return ResultVO.error(-1, "类型错误");
        }
        huifu.setContenttxt(context);
        huifu.setContenthtml(context);
        huifu.setCreateuserid(customer.getCustId());
        huifuService.insert(huifu);
        return ResultVO.success();
    }

    /**
     * 发布接口
     */
    @ResponseBody
    @RequestMapping("/publish/{id}")
    public ResultVO release(@PathVariable("id") Integer actId,
                            @RequestParam(value = "contenttxt", required = false) String contenttxt,
//                            @RequestParam(value = "img", required = false) String img,
                            @RequestParam("archivalNumber") String archivalNumber,
                            @RequestParam(value = "file", required = false) MultipartFile file) throws ParseException {
//        String actId = request.getParameter("actId");
//        String archivalNumber = request.getParameter("archivalNumber");
        List<KbrActivityCustomer> customers = activityService.findCustomer(actId, archivalNumber);
        if (CollectionUtils.isEmpty(customers)) {
            return ResultVO.error(-1, "用户未报名");
        }

        KbrActivityCustomer customer = customers.get(0);

        Huifu huifu = new Huifu();
        huifu.setContenttxt(contenttxt);
        huifu.setCreateuserid(customer.getCustId());

        if (file != null) {
            try {
                String filePath = FileUpload.upload(file);
                huifu.setImg(filePath);
            } catch (IOException e) {
                return ResultVO.error(-1, "文件上传失败");
            }
        }

        Tiezi tiezi = tieziService.selectTieziByActivityId(actId);
        huifu.setTieziid(tiezi.getId());
        huifu.setType(Constants.HuifuType.UPLOAD_FILES);

        huifuService.insert(huifu);

        return ResultVO.success(huifu);
    }

    @ResponseBody
    @RequestMapping("/upload")
    public ResultVO upload(@RequestParam(value = "actFile", required = false) MultipartFile file) throws IOException {
        String filePath = FileUpload.upload(file);
        return ResultVO.success(filePath);
    }

}
